﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml;

namespace BitworkStudios.Web.Mvc.Framework.Configuration
{
    public class ApplicationConfigSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            var configurationObject = new Hashtable();

            // Gets any attributes for this section element.
            var attributes = new Hashtable();
            foreach (XmlAttribute attrib in section.Attributes)
            {
                if (XmlNodeType.Attribute == attrib.NodeType)
                    attributes.Add(attrib.Name, attrib.Value);
            }

            // Puts the section name and attributes as the first config object item.
            configurationObject.Add(section.Name, attributes);

            // Gets the child element names and attributes.
            foreach (XmlNode child in section.ChildNodes)
            {
                if (XmlNodeType.Element == child.NodeType)
                {
                    var childAttributes = new Hashtable();

                    foreach (XmlAttribute childAttrib in child.Attributes)
                    {
                        if (XmlNodeType.Attribute == childAttrib.NodeType)
                            childAttributes.Add(childAttrib.Name, childAttrib.Value);
                    }
                    configurationObject.Add(child.Name, childAttributes);
                }
            }

            return (configurationObject);

        }
    }
}
